package com.example.dangerouscargodriver.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.entry.CompanyInfo;
import com.example.dangerouscargodriver.entry.ResourceInfo;
import com.example.dangerouscargodriver.ui.activity.CompanyDetailActivity;
import com.example.dangerouscargodriver.ui.activity.MyResourceDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class CompanyInfoAdapter extends RecyclerView.Adapter<CompanyInfoAdapter.ViewHolder>{

	private static final String TAG = "CompanyInfoAdapter";
	private List<CompanyInfo> mCompanyInfoItemList = null;
	private Context mContext;
	private ClickListener mClickListener;
	private View mRootView;

	static class ViewHolder extends RecyclerView.ViewHolder {
		private TextView mCompanyName = null;
		private TextView mCompanyClass = null;
		private ImageView mStarView = null;
		private TextView mCompanyPoint = null;
		private ImageView mMainPic = null;

		public ViewHolder(View view) {
			super(view);
			mCompanyName = view.findViewById(R.id.tv_company_name);
			mStarView = view.findViewById(R.id.iv_star_view);
			mCompanyClass = view.findViewById(R.id.tv_company_class);
			mCompanyPoint = view.findViewById(R.id.tv_company_point);
			mMainPic = view.findViewById(R.id.iv_main_pic);
		}

	}

	public CompanyInfoAdapter() {
		mCompanyInfoItemList = new ArrayList<>();
		mClickListener = new ClickListener();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		mRootView = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.layout_comapny_info_item, parent, false);
		mContext = parent.getContext();
		ViewHolder holder = new ViewHolder(mRootView);

		return holder;
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		final int selectPos = position;
		CompanyInfo companyInfo = mCompanyInfoItemList.get(position);

		holder.mCompanyName.setText(companyInfo.getName());
		holder.mCompanyClass.setText(companyInfo.getClassname());
		//设置star图片信息
		holder.mCompanyPoint.setText(companyInfo.getGrade());
		Glide.with(mContext).load(companyInfo.getMain_pic()).into(holder.mMainPic);

		holder.itemView.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				mClickListener.onClick(selectPos);
				notifyDataSetChanged();
			}
		});
	}

	@Override
	public int getItemCount() {
		return mCompanyInfoItemList.size();
	}

	public class ClickListener {
		private int mPosition = -1;

		void onClick(int position){
			mPosition = position;
			Intent intent = new Intent(mRootView.getContext(), CompanyDetailActivity.class);
			intent.putExtra("cid", mCompanyInfoItemList.get(position).getCid());
			mRootView.getContext().startActivity(intent);
		}

		public int getPosition() {

			return mPosition;
		}

		public void setPosition(int mPosition) {

			this.mPosition = mPosition;
		}
	}

	public void addData(int position, CompanyInfo item) {
		mCompanyInfoItemList.add(position, item);
		notifyItemInserted(position);
	}

	public void removeData(int position) {
		mCompanyInfoItemList.remove(position);
		notifyItemRemoved(position);
	}

	public void updateData(int position, CompanyInfo item) {
		mCompanyInfoItemList.set(position, item);
		notifyItemChanged(position);
	}

	public void clearData(){
		mCompanyInfoItemList.clear();
		notifyDataSetChanged();
	}

	public void appendData(List<CompanyInfo> array) {
		for(int index = 0; index < array.size(); index++){
			mCompanyInfoItemList.add(array.get(index));
		}
		notifyDataSetChanged();
	}
}
